﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SINGenerator
{
    public partial class AppForm : Form
    {
        private bool exit;
        private SIN sinGenerator;
        private QuipGenerator qGen;

        public AppForm()
        {
            exit = false;
            sinGenerator = new SIN();
            InitializeComponent();
            comboBox1.SelectedIndex = 0;
            qGen = new QuipGenerator();

            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            notifyIcon1.ShowBalloonTip(500, "SIN Generator", "SIN Generator has been started. Right-click this icon to generate random SINs and double-click to do more!", ToolTipIcon.Info);
        }

        #region User Interface

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            ShowApp();
        }

        private void ShowApp()
        {
            this.ShowInTaskbar = true;
            Show();
            this.BringToFront();
            WindowState = FormWindowState.Normal;
        }

        private void HideApp(FormClosingEventArgs e)
        {
            WindowState = FormWindowState.Minimized;
            Hide();
            e.Cancel = true;
        }

        private void AppForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.WindowsShutDown)
                exit = true;

            if (!exit)
                HideApp(e);
        }

        private void contextMenuStrip2_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (listView1.SelectedItems.Count <= 0)
            {
                e.Cancel = true;
            }
        }

        #endregion

        #region System Tray Context Menu

        private void toolStripMenuItem_Exit_Click(object sender, EventArgs e)
        {
            exit = true;
            Close();
        }

        private void toolStripMenuItem_GenSINWithSpaces_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(sinGenerator.GetSINSpaces());
            updateListview();
        }

        private void toolStripMenuItem_GenSINWithHyphens_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(sinGenerator.GetSINHyphens());
            updateListview();
        }

        private void toolStripMenuItem_GenSIN_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(sinGenerator.GetSIN());
            updateListview();
        }

        private void mainWindowToolStripMenuItem_Main_Window_Click(object sender, EventArgs e)
        {
            ShowApp();
        }
        
        private void AboutMenuItem_Click(object sender, EventArgs e)
        {
            About aboutDlg = new About(qGen.GetRandomQuip());
            aboutDlg.Show();
        }

        #endregion

        #region List View Methods

        public void updateListview()
        {
            if (sinGenerator == null || sinGenerator.history.Count < 1)
                return;
            //listView1.Items.Clear();

            listView1.Columns[0].Width = 111;
            listView1.Columns[1].Width = 160;

            //for (int i = 0; i < sinGenerator.history.Count; i++)
            //{
            int i = sinGenerator.history.Count - 1;
            String histStr = sinGenerator.history[i].ToString();
            String[] histStrArr = histStr.Split(';');

            ListViewItem itemSIN = new ListViewItem(histStrArr[0]);
            ListViewItem.ListViewSubItem itemTime = new ListViewItem.ListViewSubItem(itemSIN, histStrArr[1]);
            itemSIN.SubItems.Add(itemTime);
            listView1.Items.Add(itemSIN);
            //}

            if (sinGenerator.history.Count > 0)
                listView1.EnsureVisible(sinGenerator.history.Count - 1);
        }

        #endregion

        #region List View Context Menu

        private void copyWithSpacesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String temp = sinGenerator.RemoveSeparator(listView1.SelectedItems[0].Text);
            temp = sinGenerator.InsertSeparator(temp, " ");
            Clipboard.SetText(temp);
        }

        private void copyWithHyphensToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String temp = sinGenerator.RemoveSeparator(listView1.SelectedItems[0].Text);
            temp = sinGenerator.InsertSeparator(temp, "-");
            Clipboard.SetText(temp);
        }

        private void copyAsIsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String temp = sinGenerator.RemoveSeparator(listView1.SelectedItems[0].Text);
            Clipboard.SetText(temp);
        }

        #endregion

        #region Buttons Event Handlers

        private void button_GenSIN_Click(object sender, EventArgs e)
        {
            sinGenerator.GetSIN();
            updateListview();
        }

        private void button_GenCustomSIN_Click(object sender, EventArgs e)
        {
            sinGenerator.GetCustomSIN(9 - comboBox1.SelectedIndex);
            updateListview();
        }

        private void button_TestSIN(object sender, EventArgs e)
        {
            if (sinGenerator.CheckSIN(textBox_validate_SIN.Text))
                IsSINValid.Text = "Valid";
            else
                IsSINValid.Text = "Invalid";
        }

        #endregion
    }
}
